package com.example.backend.controller;

import com.example.backend.service.PostService;
import com.example.backend.util.PageUtils;
import com.example.backend.util.Result;
import com.example.backend.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author Jcy
 * @Date 2022/5/3
 */
@RestController
@RequestMapping("/v1/posts")
public class PostController {
    @Autowired
    private PostService postService;
    /**
     * 用户发帖
     * @param params 参数键值对，包括用户唯一标识和帖子内容
     * @return 根据结果返回不同信息
     */
    @PostMapping
    public Result createPost(@RequestBody Map<String, Object> params) {
        if (postService.createPost((int) params.get("userId"), (String) params.get("content")) == 0) {
            return ResultGenerator.genSuccessResult("发帖成功", null);
        }
        else {
            return ResultGenerator.genFailResult("发帖失败", null);
        }
    }
    @PostMapping("/like/{postId}")
    public Result like(@PathVariable("postId") int postId,@RequestBody Map<String, Object> params){
        if(postService.like(postId,(int) params.get("userId"))==0){
            return ResultGenerator.genSuccessResult("点赞成功", null);
        }
       else if(postService.like(postId,(int) params.get("userId"))== 1) {
            return ResultGenerator.genFailResult("已点赞此帖，请勿重复点赞", null);
        }
        else {
            return ResultGenerator.genFailResult("点赞失败", null);
        }
    }
    @PostMapping("/comments/{postId}")
    public  Result comment(@PathVariable("postId") int postId,@RequestBody Map<String, Object> params){
        if(postService.comment((int)params.get("userId"),postId,(String) params.get("comment"))==0){
            return ResultGenerator.genSuccessResult("评论成功", null);
        }
        else if(postService.comment((int)params.get("userId"),postId,(String) params.get("comment"))==1){
            return ResultGenerator.genFailResult("评论字数超出限制，请减少字数", null);
        }
        else {
            return ResultGenerator.genFailResult("评论失败", null);
        }
    }
    @DeleteMapping("/{postId}")
    public Result deletePost(@PathVariable("postId") int postId){
        if(postService.deletePost(postId)==0){
            return ResultGenerator.genSuccessResult("删帖成功", null);
        }
        else {
            return ResultGenerator.genFailResult("删帖失败", null);
        }
    }
    @PutMapping("/{postId}")
    public Result modifyPost(@PathVariable("postId") int postId,@RequestBody Map<String, Object> params){
        if(postService.modifyPost(postId,(String)params.get("content"))==0){
            return ResultGenerator.genSuccessResult("修改帖子内容成功", null);
        }
        else{
            return ResultGenerator.genFailResult("修改帖子内容失败", null);
        }
    }
    @GetMapping
    public Result getPost(int page, int pageSize){
        Map<String, Object> map = new HashMap<>();
        if(postService.getPosts(page, pageSize) != null){
            PageUtils p = postService.getPosts(page, pageSize);
            map.put("page", page);
            map.put("pageSize", pageSize);
            map.put("totalPageNum", p.getTotalPage());
            map.put("list", p.getList());
            return ResultGenerator.genSuccessResult("查看所有帖成功", map);
        }
        else{
            return ResultGenerator.genFailResult("查看所有帖失败", null);
        }
    }
    @GetMapping("/comments")
    public Result getComments(int page, int pageSize){
        Map<String, Object> map = new HashMap<>();
        if(postService.getComments(page, pageSize) != null){
            PageUtils p = postService.getComments(page, pageSize);
            map.put("page", page);
            map.put("pageSize", pageSize);
            map.put("totalPageNum", p.getTotalPage());
            map.put("list", p.getList());
            return ResultGenerator.genSuccessResult("查看评论成功", map);
        }
        else{
            return ResultGenerator.genFailResult("查看评论失败", null);
        }

    }
    @GetMapping("/{userId}")
    public Result getUserPosts(@PathVariable("userId")int userId, int page, int pageSize){
        Map<String, Object> map = new HashMap<>();
        if(postService.getUserPosts(userId,page, pageSize) != null){
            PageUtils p = postService.getUserPosts(userId,page, pageSize);
            map.put("page", page);
            map.put("pageSize", pageSize);
            map.put("totalPageNum", p.getTotalPage());
            map.put("list", p.getList());
            return ResultGenerator.genSuccessResult("查看个人所有帖成功", map);
        }
        else{
            return ResultGenerator.genFailResult("查看个人所有帖失败", null);
        }

    }

}
